package cn.wzg.server.Service;

import cn.wzg.ZKService.ServiceRegister;
import cn.wzg.ZKService.ZKpojo.ZkServiceRegister;
import lombok.Data;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;

@Data
public class ServiceProvider {
    private Map<String, Object> interfaceProvider;
    private String host;
    private int port;
    private ServiceRegister serviceRegister;


    public ServiceProvider(String host, int port) {
        this.host = host;
        this.port = port;
        this.interfaceProvider=new HashMap<>();
        this.serviceRegister=new ZkServiceRegister();
    }

    public void provideServiceInterface(Object service){
        String serviceName = service.getClass().getName();
        Class<?>[] interfaces = service.getClass().getInterfaces();

        for(Class clazz : interfaces){
            interfaceProvider.put(clazz.getName(),service);
            serviceRegister.register(clazz.getName(),new InetSocketAddress(host,port));
        }

    }

    public Object getService(String interfaceName){
        return interfaceProvider.get(interfaceName);
    }
}
